﻿using System;
using GeoAPI.Geometries;
using NetTopologySuite.Algorithm;

namespace NetTopologySuite.Geometries
{
	[Serializable]
	public class LinearRing : LineString, ILinearRing, ILineString, ICurve, IGeometry, ICloneable, IComparable, IComparable<IGeometry>, IEquatable<IGeometry>, ILineal
	{
		public LinearRing(ICoordinateSequence points, IGeometryFactory factory) : base(points, factory)
		{
			this.ValidateConstruction();
		}

		private void ValidateConstruction()
		{
			if (!this.IsEmpty && !base.IsClosed)
			{
				throw new ArgumentException("points must form a closed linestring");
			}
			if (base.CoordinateSequence.Count >= 1 && base.CoordinateSequence.Count < 4)
			{
				throw new ArgumentException("Number of points must be 0 or >3");
			}
		}

		public override Dimension BoundaryDimension
		{
			get
			{
				return Dimension.False;
			}
		}

		public override bool IsClosed
		{
			get
			{
				return this.IsEmpty || base.IsClosed;
			}
		}

		public override string GeometryType
		{
			get
			{
				return "LinearRing";
			}
		}

		public override IGeometry Reverse()
		{
			ICoordinateSequence coordinates = base.CoordinateSequence.Reversed();
			return base.Factory.CreateLinearRing(coordinates);
		}

		public bool IsCCW
		{
			get
			{
				return CGAlgorithms.IsCCW(base.CoordinateSequence);
			}
		}

		public LinearRing(Coordinate[] points) : this(Geometry.DefaultFactory.CoordinateSequenceFactory.Create(points), Geometry.DefaultFactory)
		{
		}

		public const int MinimumValidSize = 4;
	}
}
